#region Imported namespaces
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using DataAccess = PHSRAG.Utility.DataAccess;
using GenerateKey = PHSRAG.Insight21.BusinessObjects.ModuleKey;
#endregion

namespace PHSRAG.Insight21.Agreements
{
	/// <summary>
	/// Represents sponsors tied to a protocol. This class holds a collection of Sponsor objects each representing a sponsor.
	/// </summary>
	public class Sponsors : BusinessObjects.BusinessObject
	{
		#region Private instance variables
		private ArrayList sponsorList=null;
		#endregion

		#region private constant cacheKey(Sponsors)
		private static string SponsorsKey = GenerateKey.MakeKey("SponsorsKey_E3DA1268-409A-4ff2-9918-0E5EA7FCB41A",GenerateKey.Agreement);
		private const string SponsorCollectionTableName = "Sponsor";
		#endregion

		#region public properties
		/// <summary>
		/// Gets or sets the collection of sponsors
		/// </summary>
		public ArrayList SponsorList
		{
			get 
			{
				if (sponsorList==null)
				{
					sponsorList = GetList(CachePolicy);
				}
				return sponsorList;
			}
			set {sponsorList = value;}
		}

		#endregion

		#region public override methods

		#region caching
		/// <summary>
		/// Get the key to be used in all caching operations for this instance of the object.
		/// </summary>
		public static string CacheKey	{ get { return SponsorsKey; } }
		/// <summary>
		/// Cache the current instance using the prevalent cache policy.
		/// </summary>
		public override void Cache()
		{
			CachePolicy.Cache(SponsorsKey, this);
		}

		/// <summary>
		/// Remove the current instance from the cache using the prevalent cache policy.
		/// </summary>
		public override void UnCache()
		{
			CachePolicy.UnCache(SponsorsKey);
		}
		#endregion

		/// <summary>
		/// Loads all the Sponsors with the specified name, shortname.
		/// </summary>
		/// <param name="connectionString">Information to connect to the database</param>
		/// <param name="args">name, shortname</param>
		public override void Load (string connectionString,  params object[] args )
		{
			try
			{
				if (args.Length != 2)
				{
					throw new Exception("Mismatch in parameters passed for the load method of the Sponsors object");
				}
				else 
				{
               SqlParameter[] sqlParameters = DataAccess.BuildSqlParameterArray();

               if ( string.Empty != (string)args[0] )
               {
                  DataAccess.AppendToSqlParameterArray(ref sqlParameters, "@name", SqlDbType.VarChar, args[0]);
               }

               if ( string.Empty != (string)args[1] )
               {
                  DataAccess.AppendToSqlParameterArray(ref sqlParameters, "@shortName", SqlDbType.VarChar, args[1]);
               }

               

               DataContainer = DataAccess.GetDataSet(connectionString, "GetSponsorsByCriteria", sqlParameters);
               if((DataContainer!=null) && (DataContainer.Tables.Count > 0))
					{
						DataContainer.Tables[0].TableName = SponsorCollectionTableName;
					}
					else
					{
						throw new Exception("Failed to load sponsors. The stored procedure returned incorrect results.");
					}
            }						
			}
			catch(Exception ex)
			{
				throw new Exception("Sponsors.Load(): Failed to load Sponsors.", ex);
			}
		}
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new Sponsors object based on the Cache Policy passed in as a parameter. 
		/// </summary>
		/// <param name="cachePolicy">Cache Policy which specifies where the Sponsors object should be cached</param>
		public Sponsors(ICachePolicy cachePolicy): base(cachePolicy)
		{
		}
		#endregion

		#region private methods
		/// <summary>Gets a collection of sponsor objects. Each object in the collection corresponds to a row in the DataContainer.</summary>
		/// <param name="cachePolicy">Prevalent cache policy</param>
		/// <returns>Arraylist of sponsor objects</returns>
		/// <remarks>
		/// The Sponsor class is designed to construct a sponsor object with information (one single row) available in a DataSet.
		/// As such this method, imports each row from the DataContainer into a new DataSet, one at a time, and constructs the Sponsor object
		/// </remarks>
		private ArrayList GetList(ICachePolicy cachePolicy)
		{
         ArrayList sponsorList = new ArrayList();

			if ((DataContainer != null) && (DataContainer.Tables.Count != 0))
			{
				foreach (DataRow dataRow in DataContainer.Tables[SponsorCollectionTableName].Rows)
				{
               sponsorList.Add(new Sponsor(cachePolicy, dataRow));				
				}
			}

			return sponsorList;
		}

		#endregion

		#region public methods
		/// <summary>
		/// Gets all the sponsor types
		/// </summary>
		/// <param name="connectionString">Information to connect to ReX database</param>
		/// <returns>Datatable with Id, Name, Description columns</returns>
		public static DataTable GetSponsorTypes(string connectionString)
		{
			try
			{
				return DataAccess.GetDataTable(connectionString, "GetSponsorTypes", null);	
			}
			catch(Exception ex)
			{
				throw new Exception("Sponsors.GetSponsorTypes(): Failed to get the Sponsor types.", ex);
			}
		}

		/// <summary>
		/// Get rows back based on the startindex and the count 
		/// </summary>
	   public ArrayList GetSponsorsArrayList(int startIndex, int count, ICachePolicy cachePolicy)
		{			
			ArrayList sponsorList=null;
			if ((DataContainer != null) && (DataContainer.Tables.Count != 0))
			{
				sponsorList=new ArrayList();						
				for (int i = startIndex, end = Math.Min(i + count, DataContainer.Tables[SponsorCollectionTableName].Rows.Count); i < end; ++i)
				{	                           
					sponsorList.Add(new Sponsor(cachePolicy, DataContainer.Tables[SponsorCollectionTableName].Rows[i]));
				}
			}

			return sponsorList;
		}
		#endregion
	}
}
